/**
 * zmn产品模块-产品列表
 * @author tanghongjun
 * @since 2018/10/18
 */
layui.extend({
    zmnExportUtil: '{/}/static/js/zmn.excel.util'
}).define(['index', 'form', 'table', 'laypage', 'layedit','zmnExportUtil'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;
    var laypage = layui.laypage;
    var layedit = layui.layedit;
    let zmnExportUtil =layui.zmnExportUtil;

    let deptList = json.deptList;
    let deptIdName={};
    if(deptList&&deptList.length>0){
       deptList.forEach(e=>deptIdName[e.deptId]=e.deptName);
    }

    table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        page: true,
        limit: json.pageSize,
        height: 'full-149',
        done: function () {
            $("div[lay-event='LAYTABLE_EXPORT']").attr('lay-event', 'CUSTOM_EXPORT');
            let table = layui.$('#zmn-table').next("div").find('table');
            let trs = table.find('tbody tr');
            trs.each(function (i, item) {
                let tr = layui.$(item);
                let deptDiv = tr.find('td[data-field="trackDeptId"] div');
                let orgIds = deptDiv.text().split(",");
                let names = [];
                orgIds.forEach((item)=>names.push(deptIdName[item]));
                deptDiv.text(names.join(","));
            })
        },
    });
    laypage.render(layui.$.extend({limits:[10, 20, 30, 40, 50,100]},zmnPage.laypageOptions(json)));

    let ids = new Array();

    // 选择（全选或单选）
    table.on("checkbox(zmn-table)", function (obj) {
        ids = table.checkStatus('zmn-table').data.map((item) => item.trackId);
    });

    // 启用/停用
    table.on("toolbar(zmn-table)", function (obj) {
        //启用停用
        if (obj.event == "start" || obj.event == "stop") {
            if (ids.length === 0) {
                return layer.alert("请选择至少1个类型！");
            }
            zmnPage.layStartStop3(obj, "/orderconfig/track/updateStatus.action", ids.join(","),"确定停用？停用后，将不可继续发起跟单");

        }
        //导出excel
        if(obj.event=='CUSTOM_EXPORT'){
            zmnExportUtil.doExportExcel('zmn-table','跟单配置');
        }

    });

    // 修改排序和首字母
    table.on("edit(zmn-table)", function (obj) {
        if(obj.field=='sort'){
            zmnPage.laySortPositiveInteger(obj.value,"/orderconfig/track/updateSort.action",obj.data.trackId,0,0)
        }

    });
    //对外暴露的接口
    exports('track_list_js', {});
});

